package im.starchat.chat.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.starchat.chat.R;
import im.starchat.chat.impl.IProductBuyCallback;
import im.starchat.chat.rest.model.ProductBean;

/**
 * Created by liuchao on 2/10/16.
 * 直播feed流适配器
 */
public class MyDiamondAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;
    private List<ProductBean> mProductList;
    private IProductBuyCallback mProductBuyCallback;

    public MyDiamondAdapter(Context context, IProductBuyCallback productBuyCallback) {
        this.mContext = context;
        this.mProductBuyCallback = productBuyCallback;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new DiamondViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_diamond, parent, false));


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        DiamondViewHolder diamondViewHolder = (DiamondViewHolder) holder;
        diamondViewHolder.mDiamondCount.setText(mProductList.get(position).getAmount() + "");
        diamondViewHolder.mBuyBtn.setText(mProductList.get(position).getPrice() + "元");
        diamondViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mProductBuyCallback != null){
                    mProductBuyCallback.onBuy(mProductList.get(position));
                }
            }
        });
//        diamondViewHolder.mDiamondCount.setText();
    }

    public void refreshProductList(List<ProductBean> productList){
        this.mProductList = productList;
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        if (mProductList == null) {
        return 0;
        }
        return mProductList.size();
    }

    public class DiamondViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.diamond_count)
        TextView mDiamondCount;
        @InjectView(R.id.buy_btn)
        TextView mBuyBtn;

        public DiamondViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }


}
